package com.jinduplan.planmanager.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.cxz.kotlin.baselibs.utils.LogUtil;
import com.jinduplan.planmanager.R;
import com.jinduplan.planmanager.app.App;


import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.ExecutionException;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;


/**
 * Created by CZG on 2018/7/8.
 */

public class GlideUtil {


    public static void load(Activity activity, String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions().centerCrop()
                    .placeholder(R.mipmap.ic_launcher);
            Glide.with(activity).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

    public static void loadNoType(Activity activity, String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(R.mipmap.ic_launcher);
            Glide.with(activity).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

    public static void load(Activity activity, String url, ImageView iv, int cornerRadius) {
        Glide.with(iv)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(cornerRadius))
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                )
                .into(iv);
    }

    public static void load(Activity activity, String url, ImageView iv, int cornerRadius, boolean useCache) {
        Glide.with(iv)
                .load(url)
                .skipMemoryCache(true)
                .diskCacheStrategy(useCache ? DiskCacheStrategy.ALL : DiskCacheStrategy.NONE)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(cornerRadius))
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                )
                .into(iv);
    }

    public static void loadFixXY(Context context, String url, final ImageView iv) {
        RequestManager requestManager = Glide.with(context);
        requestManager.downloadOnly()
                .load(url)
                .addListener(new RequestListener<File>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
                        try {
                            FileInputStream fis = new FileInputStream(resource);
                            Bitmap bmp = BitmapFactory.decodeStream(fis);
                            iv.setImageBitmap(bmp);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return false;
                    }
                })
                .preload();
    }

    public static void load(int placeholder, Activity activity, String url, ImageView iv, int cornerRadius) {
        Glide.with(iv)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(cornerRadius))
                        .placeholder(placeholder)
                        .error(placeholder)
                )
                .into(iv);
    }

    public static void load(int placeholder, String url, ImageView iv, int cornerRadius) {
        Glide.with(iv)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(cornerRadius))
                        .skipMemoryCache(false)
                        .placeholder(placeholder)
                        .error(placeholder)
                )
                .into(iv);
    }

    public static void load(Context context, String url, ImageView iv) {
        RequestOptions requestOptions = new RequestOptions().centerCrop()
                .placeholder(R.mipmap.ic_launcher)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(context).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
    }

    public static void loadNew(Activity activity, String url, ImageView iv,int defaultDrawable) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(defaultDrawable)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
            Glide.with(activity).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

    public static void loadNew(String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions()
                    .dontAnimate()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
            Glide.with(iv.getContext()).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }



    public static void load(String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(R.mipmap.ic_launcher)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
//            Picasso.with(iv.getContext()).load(url);
            Glide.with(iv.getContext()).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

    public static void loadLocal(String url, ImageView iv) {
        Glide.with(iv.getContext()).load(new File(url));
    }

    public static File getImgFileFromPath(String url) {
        FutureTarget<File> fileFutureTarget = Glide.with(App.Companion.getContext()).load(url).downloadOnly(1200, 1200);
        File file = null;
        try {
            file = fileFutureTarget.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static void loadFitCenter(String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            Glide.with(iv.getContext()).load(url).apply(new RequestOptions().fitCenter().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)).into(iv);
        }
    }

    public static void load(int placeholder, String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(placeholder)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(iv.getContext()).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

    public static void loadSetError(int placeholder, String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(placeholder)
                    .error(placeholder)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(iv.getContext()).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

    public static void load(String url, ImageView iv, int error) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(R.mipmap.ic_launcher)
                    .dontAnimate()
                    .error(error)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(iv.getContext()).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

    public static void load(Activity activity, String url, ImageView iv, RequestListener listener) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(iv.getContext()).applyDefaultRequestOptions(requestOptions).load(url).listener(listener).into(iv);
    }

    public static void load(Context context, String url, ImageView iv, RequestListener listener) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .fitCenter()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(iv.getContext()).applyDefaultRequestOptions(requestOptions).load(url).listener(listener).into(iv);
    }


    public static void load(Activity activity, int res, ImageView iv) {
        Glide.with(activity).load(res).into(iv);
    }

    public static Target loadGif(Activity activity, String url, ImageView iv, boolean isPlay) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        if (isPlay) {
            return Glide.with(activity).applyDefaultRequestOptions(requestOptions).asGif().load(url).into(iv);
        } else {
            return Glide.with(activity).applyDefaultRequestOptions(requestOptions).asBitmap().load(url).into(iv);
        }
    }

    public static Target loadGif(Activity activity, String url, ImageView iv, RequestListener listener) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        return Glide.with(activity).applyDefaultRequestOptions(requestOptions).asGif().listener(listener).load(url).into(iv);
    }

   /* public static Target loadGif(Activity activity, int res, GlideDrawableImageViewTarget iv, RequestListener listener) {
        return Glide.with(activity).load(res).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).listener(listener).into(iv);
    }*/

    public static Target loadGif(Activity activity, int res, ImageView iv) {
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        return Glide.with(activity).applyDefaultRequestOptions(requestOptions).asGif().load(res).into(iv);
    }

    public static void loadVideo(Context context, String videoUrl, ImageView iv) {
        Glide.with(context)
                .load(videoUrl)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transition(withCrossFade())
                .transform(new CenterCrop())
                .into(iv);
    }

    public static void loadGifInTimes(ImageView iv, Object res, RequestListener listener) {
        Glide.with(iv)
                .asGif()
                .apply(new RequestOptions().skipMemoryCache(true))
                .load(res)
                .listener(listener).into(iv);

    }

    public static void loadNoPlaceholder(Activity activity, String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            RequestOptions requestOptions = new RequestOptions().centerCrop();

            Glide.with(activity).applyDefaultRequestOptions(requestOptions).load(url).into(iv);
        }
    }

}
